package com.qt.socket.tcpwork.work2;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Author zsf
 * @Date 2023/10/5 15:18
 * @Version 1.0
 */
public class TcpSendClient {

    /**
     * TCP通信练习2---接收和反馈
     * 客户端:发送一条数据，接收服务端反馈的消息并打印服务器:接收数据并打印，再给客户端反馈消息
     */

    public static void main(String[] args) throws IOException {



        Scanner sc = new Scanner(System.in);
        Socket socket = new Socket("127.0.0.1", 8888);
        OutputStream os = socket.getOutputStream();

        InputStreamReader isr = new InputStreamReader(socket.getInputStream());

        while (true) {
            System.out.println();
            System.out.println("请输入要发送的数据:");
            String str = sc.nextLine();
            os.write(str.getBytes());

            socket.shutdownOutput();

            int len;
            System.out.println("服务端反馈的消息:");
            while ((len = isr.read()) != -1){
                System.out.print((char)len);
            }

            if("886".equals(str)){
                break;
            }
        }
        socket.close();


    }


}
